// compontnts/queryform/lt-queryForm.js
const defaultSettings = require('../../config/defaultSettings')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    queryFormFields:{
      type:Array,
      value:[]
    },
    queryFormData:{
      type:Object,
      value:{}
    },
    queryFormId:{
      type:String,
      value:"queryForm"
    },
  
  },
  lifetimes: {
    ready() {
      this.data.localQueryFormData = Object.assign({},this.data.queryFormData||{})
    },
    
  },
  /**
   * 组件的初始数据
   */
  data: {
    stSign:"_st",
    etSign:"_et",
    primaryColor:defaultSettings.primaryColor,
    localQueryFormData:{},
    localQueryFormVal:[],
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onChange(e){
      // console.log(e)
      let {field,val} = e.detail
      this.data.localQueryFormData[field] = val
      this.triggerEvent("change",{field:field,val:val})
    },
    reset(){ 
      this.setData({localQueryFormData:{}})
      this.query()
    },
    verify(){
      let flag = true
      let queryFormVal = []
      for(let item of this.data.queryFormFields){
        if(item['condition']=='between'){
          if(!this.selectComponent("#"+this.data.queryFormId+'-'+item["field"]+'-'+this.data.stSign).verify()){
            flag = false
          }else if(this.data.localQueryFormData[item['field']+this.data.stSign]){
            let tempItem = {}
            tempItem['field'] = item['field']
            tempItem['val'] = this.data.localQueryFormData[item['field']+this.data.stSign]
            tempItem['condition'] = "ge"
            queryFormVal.push(tempItem)
          }
          if(!this.selectComponent("#"+this.data.queryFormId+'-'+item["field"]+'-'+this.data.etSign).verify()){
            flag = false
          }else if(this.data.localQueryFormData[item['field']+this.data.stSign]){
            let tempItem = {}
            tempItem['field'] = item['field']
            tempItem['val'] = this.data.localQueryFormData[item['field']+this.data.stSign]
            tempItem['condition'] = "ge"
            queryFormVal.push(tempItem)
            
          }

        }else{
          if(!this.selectComponent("#"+this.data.queryFormId+'-'+item["field"]).verify()){
            flag = false
          }else if(this.data.localQueryFormData[item['field']]){
            let tempItem = {}
            tempItem['field'] = item['field']
            tempItem['val'] = this.data.localQueryFormData[item['field']]
            tempItem['condition'] = item['condition'] || "eq"
            queryFormVal.push(tempItem)
          }
        }
      }
      if(flag){
        this.data.localQueryFormVal=queryFormVal
      }else{
        this.data.localQueryFormVal=[]
      }
      return flag
    },
    query(){
      if(this.verify()){
        this.triggerEvent('searchQuery')
      }
    },
    getQueryFormVal(){
      this.verify()
      return this.data.localQueryFormVal
    }
  }
})
